﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MLY.Dal;

namespace MLY.Bll
{
    public class Bl_Dist
    {
        public static TableWorkOrder DuplicateWo(TableWorkOrder t)
        {
            TableWorkOrder workOrder = new TableWorkOrder();
            workOrder.ActualDelivaryDate = t.ActualDelivaryDate;
            workOrder.ContractorCatalogueCode = t.ContractorCatalogueCode;
            workOrder.ContractorId = t.ContractorId;
            workOrder.ContractorLotNumber = t.ContractorLotNumber;
            workOrder.ContractorWorkOrderNumber = t.ContractorWorkOrderNumber;
            workOrder.CustomerCatalougeCode = t.CustomerCatalougeCode;
            workOrder.CustomerId = t.CustomerId;
            workOrder.TextFieldA = t.TextFieldA;
            workOrder.TextFieldB = t.TextFieldB;
            workOrder.TextFieldC = t.TextFieldC;
            workOrder.DateFieldA = t.DateFieldA;
            workOrder.DateFieldB = t.DateFieldB;
            workOrder.SendDate = t.SendDate;
            workOrder.SplitId = t.SplitId;
            workOrder.CustomerPriority = t.CustomerPriority;
            workOrder.CustomerWorkOrderNumber = t.CustomerWorkOrderNumber;

            // CR 12-02-2011 Matti: Distribution.
            //DateCreated = t.DateCreated,
            workOrder.DateCreated = DateTime.Now;

            workOrder.OrderNumber = t.OrderNumber;
            workOrder.PlannedDelivaryDate = t.PlannedDelivaryDate;
            workOrder.ProductDescription = t.ProductDescription;
            workOrder.Quantity = t.Quantity;
            workOrder.Remarks = t.Remarks;
            workOrder.RequestedDelivaryDate = t.RequestedDelivaryDate;
            workOrder.TotalSum = t.TotalSum;

            // CR 12-02-2011 Matti: Distribution.
            //UpdateBy = t.UpdateBy,
            //UpdateDate = t.UpdateDate,
            workOrder.UpdateBy = Bll.clsUser.CurrentUserId;
            workOrder.UpdateDate = DateTime.Now;
            workOrder.originalWoForDist = t.WorkOrderId;
            workOrder.Project = t.Project;
            workOrder.disqualifyDate = t.disqualifyDate;
            workOrder.originalActualDeliveryDate = t.originalActualDeliveryDate;
            workOrder.originalWOForDisqualify = t.originalWOForDisqualify;

            return workOrder;
        }

        public static void editDist(string woId, string sentQuant, string actualDate, string plannedDate)
        {
            DateTime d = new DateTime();
            using (Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext())
            {
                Dal.TableWorkOrder wo = dc.TableWorkOrders.Where(S => S.WorkOrderId.ToString().Equals(woId)).FirstOrDefault();
                decimal? diff = decimal.Parse(sentQuant.ToString()) - wo.Quantity;
                wo.Quantity = decimal.Parse(sentQuant.ToString());
                Dal.TableWorkOrder original = dc.TableWorkOrders.Where(S => S.WorkOrderId.ToString().Equals(wo.originalWoForDist) && S.originalWoForDist == null).FirstOrDefault();

                if (DateTime.TryParse(plannedDate, out d))
                    wo.PlannedDelivaryDate = d;
                if (DateTime.TryParse(actualDate, out d))
                {
                    if (wo.ActualDelivaryDate == null)
                    {
                        original.Quantity -= wo.Quantity;
                    }
                    else
                    {
                        original.Quantity -= diff.HasValue ? diff.Value : 0;
                    }
                    wo.ActualDelivaryDate = d;
                }
                else
                {
                    if (String.IsNullOrEmpty(actualDate) && wo.ActualDelivaryDate != null)
                    {
                        wo.ActualDelivaryDate = null;
                        original.Quantity += wo.Quantity;
                    }
                }

                if (original.Quantity < 0)
                {
                    original.originalQuantity += (-1) * original.Quantity;
                    original.Quantity = 0;
                }
                dc.SubmitChanges();
            }
        }

        public static List<Dal.TableWorkOrder> getDist(int woId)
        {
            using (Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext())
            {
                Dal.TableWorkOrder wo = dc.TableWorkOrders.Where(S => S.WorkOrderId == woId).FirstOrDefault();
                List<Dal.TableWorkOrder> allDists = new List<MLY.Dal.TableWorkOrder>();
                allDists.Add(wo);
                allDists.AddRange(dc.TableWorkOrders.Where(S => S.originalWoForDist == woId).ToList());
                return allDists;
            }
        }

  
    }
}
